import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/device/entrance_guard/api.dart";
import "package:ct_pa_majordomo/pages/device/entrance_guard/index.dart";
import "package:ct_pa_majordomo/pages/widgets/menu_block.dart";
import "package:ct_pa_majordomo/utils/index.dart";
import "package:ct_pa_majordomo/utils/stacker.dart";
import "package:ct_pa_majordomo/widgets/button/index.dart";
import "package:ct_pa_majordomo/widgets/normal_scaffold/index.dart";
import "package:ct_pa_majordomo/widgets/tai_dialog/index.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:flutter_easyloading/flutter_easyloading.dart";

class Setting extends StatefulWidget {
  final Map? info;
  const Setting({super.key, this.info});

  @override
  State<StatefulWidget> createState() => _SettingState();

}

class _SettingState extends State<Setting> {
  Map _params = {
    "faceLockName": "",
    "faceLockMac": "",
    "faceLockSn": ""
  };

  bool checkIsNext () {
    return Utils.isNotEmpty(_params["faceLockName"])
        && Utils.isNotEmpty(_params["faceLockMac"])
        && Utils.isNotEmpty(_params["faceLockSn"]);
  }


  void _editName() async {
    taiDialog(
      context: context,
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 10),
        child: TextFormField(
          autofocus: true,
          decoration: const InputDecoration(
            hintText: "请输入",
            border: InputBorder.none,
          ),
          initialValue: _params["faceLockName"],
          onChanged: (val) {
            setState(() {
              _params["faceLockName"] = val;
            });
          },
        ),
      ),
      onConfirm: (taiContext) {
        _updateEntranceGuard(taiContext);
      }
    );
  }

  void _updateEntranceGuard(BuildContext taiContext) async {
    Map<String, dynamic> params = {
      "faceLockId": _params["faceLockId"],
      "faceLockName": _params["faceLockName"],
    };
    await entranceGuardApi.editEntranceGuard(params);
    EasyLoading.showToast("修改成功");
    if (taiContext.mounted) {
      Navigator.of(taiContext).pop();
    }
  }

  @override
  void initState() {
    setState(() {
      _params = widget.info ?? _params;
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return NormalScaffold(
      title: "设置",
      child: ListView(
        children: [
          MenuBlock(
            borderRadius: const BorderRadius.all(Radius.circular(0)),
            items: [
              MenuBlockItem(
                text: "门禁名称",
                showArrow: true,
                suffix: Text(_params["faceLockName"]),
                onTap: () {
                  _editName();
                }
              ),
              MenuBlockItem(
                  // prefix: const Text("*", style: TextStyle(color: ThemeVars.colorError),),
                  text: "门禁MAC码",
                  showArrow: false,
                  suffix: Text(_params["faceLockMac"]),
              ),
              MenuBlockItem(
                  // prefix: const Text("*", style: TextStyle(color: ThemeVars.colorError),),
                  text: "门禁SN码",
                  showArrow: false,
                  suffix: Text(_params["faceLockSn"]),
              ),
            ]
          ),
          Padding(
            padding: const EdgeInsets.only(left: ThemeVars.paddingPage, right: ThemeVars.paddingPage, top: 60),
            child: Button(
              type: ButtonType.dangerOutline,
              onPressed: () {
                taiDialog(
                  context: context,
                  child: const Text("确定删除门禁？", textAlign: TextAlign.center,),
                  onConfirm: (taiContext) async {
                    await entranceGuardApi.removeEntranceGuard(_params["faceLockId"]);
                    EasyLoading.showToast("删除成功");
                    if (taiContext.mounted) {
                      Navigator.of(taiContext).pop();
                      Navigator.pop(context);
                    }
                  }
                );
              },
              child: const Text("删除")
            ),
          ),
        ],
      ),
    );
  }

}